import psutil
import json
import re
import subprocess
import time
import os
import multiprocessing


def getProcess(process_name):
    p = subprocess.Popen('/usr/bin/ffmpeg -nostdin -re -rtsp_transport tcp -i rtsp://36.133.66.40:8554/' + process_name + ' -c:v libx264 -f flv rtmp://192.168.0.4:1935/live/' + process_name + ' &', stdout=subprocess.PIPE, shell=True)
    p.

def getData():
    allsite = []
    i = 0
    with open("site.json", 'r', encoding="utf-8") as f:
        line = f.readline()
        while line:
            if line != None:
                # if i > 20:
                #    break
                content = line.lstrip().replace(" ", "").replace(",", "")
                if content.find("cdtf") > 0:
                    pattern = r'\"(.*?)\"'
                    matches = re.findall(pattern, content)
                    allsite.append(matches[1])
                    i += 1
            line = f.readline()
        return allsite


def worker(allsite):
    for site in allsite:
        piece = str(site).split("/")
        flowname = piece[3]
        try:
            getProcess(flowname)
        except:
            pass


if __name__ == "__main__":
    allsite = getData()
    newsite = []
    newsite1 = []
    newsite2 = []
    newsite3 = []
    newsite4 = []
    newsite5 = []
    newsite6 = []
    newsite7 = []
    newsite8 = []
    newsite9 = []
    m = 0
    for site in allsite:
        val = m % 10
        if val == 0:
            newsite.append(site)
        elif val == 1:
            newsite1.append(site)
        elif val == 2:
            newsite2.append(site)
        elif val == 3:
            newsite3.append(site)
        elif val == 4:
            newsite4.append(site)
        elif val == 5:
            newsite5.append(site)
        elif val == 6:
            newsite6.append(site)
        elif val == 7:
            newsite7.append(site)
        elif val == 8:
            newsite8.append(site)
        elif val == 9:
            newsite9.append(site)
        m += 1

    # 创建进程对象
    process = multiprocessing.Process(target=worker, args=(newsite,))
    process1 = multiprocessing.Process(target=worker, args=(newsite1,))
    process2 = multiprocessing.Process(target=worker, args=(newsite2,))
    process3 = multiprocessing.Process(target=worker, args=(newsite3,))
    process4 = multiprocessing.Process(target=worker, args=(newsite4,))
    process5 = multiprocessing.Process(target=worker, args=(newsite5,))
    process6 = multiprocessing.Process(target=worker, args=(newsite6,))
    process7 = multiprocessing.Process(target=worker, args=(newsite7,))
    process8 = multiprocessing.Process(target=worker, args=(newsite8,))
    process9 = multiprocessing.Process(target=worker, args=(newsite9,))

    # 启动进程
    process.start()
    process1.start()
    process2.start()
    process3.start()
    process4.start()
    process5.start()
    process6.start()
    process7.start()
    process8.start()
    process9.start()

    # 等待进程完成
    process.join()
    process1.join()
    process2.join()
    process3.join()
    process4.join()
    process5.join()
    process6.join()
    process7.join()
    process8.join()
    process9.join()

    print("All processes finished execution")
